package com.mdinic.blogovi.front.page.blogpost;

import java.util.Date;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.mdinic.blogovi.front.common.BDateTimeLabel;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.person.PersonViewPage;
import com.mdinic.blogovi.model.BlogPost;
import com.mdinic.blogovi.model.Comment;
import com.mdinic.blogovi.model.Person;
import com.mdinic.blogovi.service.BlogPostService;

public class BlogPostViewPanel extends Panel {

    private static final String ID = "panel";

    private static final long serialVersionUID = 1L;

    @SpringBean
    private transient BlogPostService blogPostService;

    public BlogPostViewPanel(String blogPostId, final PageParameters parameters) {
        super(ID);

        final BlogPost blogPost = BlogPost.findBlogPost(Long.valueOf(blogPostId));
        blogPost.incView();

        add(new SingleBlogPostPanel(blogPost));

        // list comments
        Comment comment = new Comment();
        comment.setPerson(BSession.get().getPerson());
        comment.setPost(blogPost);

        final ListView<Comment> comments = new ListView<Comment>("comments", new ListModel<Comment>(Comment
                .findCommentsByPost(blogPost).getResultList())) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<Comment> item) {
                // item.setModel(new
                // CompoundPropertyModel<Comment>(item.getModelObject()));

                PageParameters userParameters = new PageParameters();
                userParameters.add(BlogPostViewPage.URL_PARAM_NAME, item.getModelObject().getPerson().getId());
                BookmarkablePageLink<PersonViewPage> personLink = new BookmarkablePageLink<PersonViewPage>("user_link",
                        PersonViewPage.class, userParameters);
                personLink
                        .add(new Label(Person.NICKNAME, new PropertyModel<String>(item.getModel(), "person.nickname")));
                item.add(personLink);

                item.add(new Label(Comment.TEXT, new PropertyModel<String>(item.getModelObject(), Comment.TEXT)));
                // item.add(new Label("person.nickname"));
                item.add(new BDateTimeLabel(Comment.DATE, new PropertyModel<Date>(item.getModelObject(), Comment.DATE)));
            }
        };
        comments.setOutputMarkupId(true);

        // add comment
        final IModel<Comment> commentModel = new CompoundPropertyModel<Comment>(comment);
        Form<Comment> form = new Form<Comment>("form", commentModel) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit() {
                blogPostService.addComment(commentModel.getObject());
                setResponsePage(getPage().getClass(), parameters);
            }

        };
        final TextArea<String> textArea = new TextArea<String>(Comment.TEXT);
        final Button addCommentButton = new Button("addComment");
        // addCommentButton.setEnabled(false);
        textArea.setOutputMarkupId(true);
        addCommentButton.setOutputMarkupId(true);
        // textArea.add(new
        // AjaxFormComponentUpdatingBehavior("onkeyup inputchange change") {
        //
        // private static final long serialVersionUID = 1L;
        //
        // @Override
        // protected void onUpdate(AjaxRequestTarget target) {
        // addCommentButton.setEnabled(textArea.getModelObject() != null &&
        // textArea.getModelObject().length() > 0);
        // target.add(addCommentButton);
        // }
        //
        // });

        form.add(textArea.setRequired(true));
        form.setVisible(BSession.get().isLoggedIn());

        form.add(addCommentButton);
        add(comments);
        add(form);
    }
}
